package com.upay.android.client.activity;

import static com.upay.android.client.helper.Keys.GROUPID;
import static com.upay.android.client.helper.Keys.NEWGROUPUSERID;
import static com.upay.android.client.helper.Keys.TOKEN;
import static com.upay.android.client.helper.Keys.UPAY_PREFERENCES;
import static com.upay.android.client.helper.Keys.USERID;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.type.TypeReference;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.upay.android.client.R;
import com.upay.android.client.handler.CallbackHandler;
import com.upay.android.client.handler.EmptyResponseCallbackHandler;
import com.upay.android.client.handler.GroupAvailableUsersCallbackHandler;
import com.upay.android.core.service.RestService;
import com.upay.android.domain.rest.RestRequest;
import com.upay.android.util.rest.RequestMethod;
import com.upay.android.util.rest.UrlHelper;
import com.upay.domain.account.User;
import com.upay.domain.response.EmptyResponse;
import com.upay.domain.response.ResponseEnvelope;

public class GroupAvailableUsersActivity extends AbstractUPayActivity {

	/* ---- Constants ---- */
	public static final int MENU_SIGNOUT = 0; 

	/* ---- Instance Variables ---- */
    private Long groupId = null;
	private final CallbackHandler<List<User>> groupAvailableUsersCallbackHandler = new GroupAvailableUsersCallbackHandler(this);
	private final CallbackHandler<EmptyResponse> emptyResponseCallbackHandler = new EmptyResponseCallbackHandler(this);

	
	/* ---- Constructors ---- */
	
	/* ---- Business Methods ---- */
	

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        this.groupId = getIntent().getExtras().getLong(GROUPID);
        ((GroupAvailableUsersCallbackHandler)groupAvailableUsersCallbackHandler).setGroupId(this.groupId);
        
        SharedPreferences sharedPreferences = getSharedPreferences(UPAY_PREFERENCES, 0);
   	    ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
   	    params.add(new BasicNameValuePair(USERID, sharedPreferences.getString(USERID, null)));
   	    params.add(new BasicNameValuePair(TOKEN, sharedPreferences.getString(TOKEN, null)));
   	    params.add(new BasicNameValuePair(GROUPID, groupId.toString()));
   	    
   	    RestRequest restRequest = new RestRequest(RequestMethod.POST, UrlHelper.GROUP_AVAILABLE_USERS, params);
   	    TypeReference<ResponseEnvelope<List<User>>> typeReference = new TypeReference<ResponseEnvelope<List<User>>>() {};
   	     
   	    // Execute Request
   	    RestService<List<User>> restService = new RestService<List<User>>(restRequest, typeReference, groupAvailableUsersCallbackHandler);
   	    restService.execute();
   	    
   	    setContentView(R.layout.groupavailableusers);
   	   
    }
	
	public OnItemClickListener getMenuListener() {
	    return new OnItemClickListener() {
		    @Override
		    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			
			    User user = ((GroupAvailableUsersCallbackHandler)groupAvailableUsersCallbackHandler).getUsers().get(position);
	            SharedPreferences sharedPreferences = getContext().getSharedPreferences(UPAY_PREFERENCES, 0);
	   	        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
	   	        params.add(new BasicNameValuePair(USERID, sharedPreferences.getString(USERID, null)));
	   	        params.add(new BasicNameValuePair(TOKEN, sharedPreferences.getString(TOKEN, null)));
	   	        params.add(new BasicNameValuePair(GROUPID, groupId.toString()));
	   	        params.add(new BasicNameValuePair(NEWGROUPUSERID, user.getUserId()));
	   	    
	   	        RestRequest restRequest = new RestRequest(RequestMethod.POST, UrlHelper.GROUP_ADD_USER, params);
	   	        TypeReference<ResponseEnvelope<EmptyResponse>> typeReference = new TypeReference<ResponseEnvelope<EmptyResponse>>() {};
	   	     
	   	        // Execute Request
	   	        RestService<EmptyResponse> restService = new RestService<EmptyResponse>(restRequest, typeReference, emptyResponseCallbackHandler);
	   	        restService.execute();
	   	    				
			    getContext().finish();
		    }	
		};
	}
	
	/* ---- Getters and Setters ---- */


	public Long getGroupId() {
		return groupId;
	}


	public void setGroupId(Long groupId) {
		this.groupId = groupId;
	}
	
}
